import getLodop from '@/lodop/LodopFuncs';

// 打印中按钮控制
export const printingDisabledCtrl = (printing) => {
  const btns = document.querySelectorAll('[data-btn-type="print"]')
  if (btns && btns.length) {
    btns.forEach(item => {
      if (printing) {
        item.classList.add('is-disabled', 'pe-n')
      } else {
        item.classList.remove('is-disabled', 'pe-n')
      }
    })
  }
}

// 延时promise
export const getLoadPromise = (time) => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve();
    }, time || 1000);
  });
}

let LODOP = null;
export const lodoper = {
  init(config = {}) {
    if (!LODOP) {
      LODOP = getLodop();
    }
    const { strTaskName, intOrient, pageWidth, pageHeight, strPageName, printer, fileName } = config;
    LODOP.PRINT_INIT(strTaskName || ''); //初始化
    LODOP.SET_PRINT_PAGESIZE(intOrient || 0, pageWidth || '101.6mm', pageHeight || '152mm', strPageName || 'USER');
    if (printer) {
      LODOP.SET_PRINTER_INDEX(printer); // 指定打印机
    }
    if (fileName) {
      LODOP.SET_PRINT_MODE('CUSTOM_TASK_NAME', fileName)// 文档名
    }
  },
  print(type) {
    return new Promise(resolve => {
      CLODOP.On_Return=function(TaskID,value){
        resolve(value)
      }
      LODOP[type] && LODOP[type]()
    })
  },
  buildPage(el, config = {}) {
    const { top, left, width, height } = config;
    LODOP.NewPage();
    const htmlString = el.outerHTML;
    LODOP.ADD_PRINT_HTM(top || '0', left || '0', width || '100%', height || '100%', htmlString);
  },
}